
package com.jf.cloud.order.listener;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.order.feign.OrderFeignClient;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.bo.OrderStatusBO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.order.mapper.OrderMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.GROUP_ORDER_SUCCESS_TOPIC, consumerGroup = RocketMqConstant.GROUP_ORDER_SUCCESS_TOPIC)
public class GroupOrderSuccessConsumer implements RocketMQListener<List<Long>> {
    private final Logger LOGGER = LoggerFactory.getLogger(GroupOrderSuccessConsumer.class);

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 团购订单成团了，将订单状态变为待发货
     *
     * @param orderIds
     */
    @Override
    public void onMessage(List<Long> orderIds) {
        if (CollUtil.isEmpty(orderIds)) {
            LOGGER.info("团购订单成团了，将订单状态变为待发货, orderIds is empty");
            return;
        }
        ServerResponseEntity<List<OrderStatusBO>> ordersStatusResponse = orderFeignClient.getOrdersStatus(orderIds);
        if (!ordersStatusResponse.isSuccess()) {
            LOGGER.error("团购订单成团了，将订单状态变为待发货, 获取订单状态失败, orderIds: {}, msg: {}", orderIds, ordersStatusResponse.getMsg());
            throw new LuckException(ordersStatusResponse.getMsg());
        }
        orderMapper.updateGroupOrderSuccessStatus(ordersStatusResponse.getData().get(0).getOrderMold(), orderIds);
    }
}
